package ru.jkff.jalgebra.core;

/**
 * User: jkff
 * Date: Aug 23, 2009
 * Time: 12:43:14 PM
 */
public abstract class PairFunctions {
    private PairFunctions() {}

    public static <X,Y> Pair<X,Y> of(X x, Y y) {
        return new Pair<X,Y>(x,y);
    }

    public static <X,Y> Function<Pair<X,Y>, X> first() {
        return new Function<Pair<X, Y>, X>() {
            public X apply(Pair<X, Y> pair) { return pair.first; }
        };
    }

    public static <X,Y> Function<Pair<X,Y>, Y> second() {
        return new Function<Pair<X, Y>, Y>() {
            public Y apply(Pair<X, Y> pair) { return pair.second; }
        };
    }

    public static <X,Y> Function<Y, Pair<X,Y>> withFirst(final X x) {
        return new Function<Y, Pair<X, Y>>() {
            public Pair<X, Y> apply(Y y) { return of(x, y); }
        };
    }

    public static <X,Y> Function<X, Pair<X,Y>> withSecond(final Y y) {
        return new Function<X, Pair<X, Y>>() {
            public Pair<X, Y> apply(X x) { return of(x, y); }
        };
    }
}
